package de.eridea.learning.java.usermanagement.logic.pwdverification;

public class Password {
	private char[] password;

	public Password(char[] password) {
		if (password == null) {
			throw new IllegalArgumentException("Password must not be null");
		}
		this.password = new char[password.length];
		for (int k = 0; k < password.length; k++) {
			this.password[k] = password[k];
			password[k] = 0;
		}
	}

	public char[] getCharacters() {
		return password;
	}

	public void erase() {
		if (password != null) {
			for (int k = 0; k < password.length; k++) {
				password[k] = 0;
			}
			password = null;
		}
	}

	public String toString() {
		return (password != null) ? "*(password#"
				+ Integer.toString(hashCode(), 16) + ")*****" : "x(password#"
				+ Integer.toString(hashCode(), 16) + ")ERASEDxx";
	}
}
